Page({
  data: {
    register: false,
    Height: null,
    Width: null,
    categoryTitle: '分类排行',
    newTitle: '新发布',
    isLine: true,
    authorTitle: '读者排行',
    //分类
    categoryArr: [],
    //读者
    readerArr: [],
    //新发布
    newArr: [],
    popularArr: [{
        id: 1,
        text: '2020排行榜',
        tagColor: '#F54D82'
      },
      {
        id: 2,
        text: '职场热门',
        tagColor: '#59B080'
      },
      {
        id: 3,
        text: '都市言情',
        tagColor: '#EC575E'
      },
      {
        id: 4,
        text: '哈佛大学经济管理类',
        tagColor: '#9060E8'
      },
      {
        id: 5,
        text: '领导才能',
        tagColor: '#DED658'
      },
      {
        id: 6,
        text: '电竞',
        tagColor: '#DD001B'
      }

    ],
    //热销
    salesArr: []
  },
  //事件处理函数

  onLoad: function (options) {
    this.getWidthOrHeight()
    this.getSalesArr()
    this.getNewBookList()
    this.getReaderArr()
    this.getCategoryAll()
  },
  getSalesArr() {
    const that = this
    const pageOption = {
      pageSize: 1,
      pageNum: 10,
    }
    wx.$http.get(wx.$get.getBookList,
      pageOption
    ).then(res => {
      if (res.code === 200) {
        let list = res.data.result.data
        that.setData({
          salesArr: list
        })
      }
    })
  },
  getReaderArr() {
    const pageOption = {
      pageSize: 1,
      pageNum: 100000,
    }
    wx.$http.get(wx.$get.getWxUserAll, pageOption).then(res => {
      if (res.code === 200) {
        let list = res.data.data
        list.map(item => {
          delete item.token
        })
        this.setData({
          readerArr: list
        })
      }

    })
  },
  getNewBookList() {
    const that = this
    const pageOption = {
      pageSize: 1,
      pageNum: 6,
    }
    wx.$http.get(wx.$get.getBookList,
      pageOption
    ).then(res => {
      if (res.code ===200) {
        let list = res.data.result.data
        that.setData({
          newArr: list
        })
      }
    
    })
  },
  getCategoryAll() {
    const pageOption = {
      pageSize: 1,
      pageNum: 20,
    }
    wx.$http.get(wx.$get.getBookCategory, pageOption).then(res => {
      if (res.code === 200) {
        let list = res.data.data.data
        list.map(item => {
          delete item.category_cover
          delete item.category_other
        })
        this.setData({
          categoryArr: list
        })
        wx.$cache.set('category', list)
      }
    })

  },
  goClassify() {
    wx.navigateTo({
      url: '/pages/classify/classify',
    })
  },
  goCategory() {
    wx.navigateTo({
      url: '/pages/category/category',
    })
  },
  goIntroduce(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/introduce/introduce?id=' + id,
    })
  },
  goAuthor() {
    let token = wx.$cache.get('token')
    if (!token) {
      this.setData({
        register: true
      })
      return
    }
    wx.navigateTo({
      url: '/pages/author/author',
    })
  },
  goWriter(e) {
    let token = wx.$cache.get('token')
    if (!token) {
      this.setData({
        register: true
      })
      return
    }
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/writer/writer?id=' + id,
    })
  },
  getWidthOrHeight() {
    const that = this
    wx.getSystemInfo({
      success(res) {
        that.setData({
          Height: res.windowHeight,
          Width: res.windowWidth
        })
      },
    })
  },
  goSearch() {
    let token = wx.$cache.get('token')
    if (!token) {
      this.setData({
        register: true
      })
      return
    }
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },

})